package com.xbongbong.pro.sms.model.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.sms.model.SmsConfigModel;
import com.xbongbong.saas.constant.SmsSetConstant;
import com.xbongbong.saas.domain.dao.SmsConfigDao;
import com.xbongbong.saas.domain.entity.SmsConfigEntity;
import com.xbongbong.saas.enums.SmsSignStatusEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author hongxiao
 * @dat: 2019-01-12 16:16
 */
@Service("smsConfigModel")
public class SmsConfigModelImpl implements SmsConfigModel {
    @Resource
    private SmsConfigDao smsConfigDao;

    @Override
    public Integer insert(SmsConfigEntity entity) {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);

        return smsConfigDao.insert(entity);
    }

    @Override
    public Integer update(SmsConfigEntity entity) {
        entity.setUpdateTime((long) DateUtil.getInt());

        return smsConfigDao.update(entity);
    }

    @Override
    public void updateBatch(List<SmsConfigEntity> list) {
        if (CollectionsUtil.isEmpty(list)) {
            return;
        }
        smsConfigDao.updateBatch(list);
    }

    @Override
    public Integer save(SmsConfigEntity entity) {
        if (entity.getId() == null || entity.getId().equals(0L)) {
            return smsConfigDao.insert(entity);
        }
        return smsConfigDao.update(entity);
    }

    @Override
    public Integer deleteByKey(Long key) {
        return smsConfigDao.deleteByKey(key);
    }

    @Override
    public SmsConfigEntity getByKey(Long key) {
        return smsConfigDao.getByKey(key);
    }

    @Override
    public List<SmsConfigEntity> findEntitys(Map<String, Object> param) {
        return smsConfigDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return smsConfigDao.getEntitysCount(param);
    }

    @Override
    public void deductFee(String corpid, String content) {
        Integer counts = 1;
        if (content.length() > SmsSetConstant.SMS_MAX_LENGTH) {
            /*
             * 长短信67字一条,防止整除时多加1
             */
            counts = content.length() / SmsSetConstant.SMS_REAL_LENTTH;
            counts += (content.length() % SmsSetConstant.SMS_REAL_LENTTH == 0) ? 0 : 1;
        }
        Map<String, Object> param = new HashMap<>(16);
        param.put("del", DelEnum.NORMAL.getDel());
        param.put("corpid", corpid);
        param.put("start", 0);
        param.put("pageNum", 1);

        SmsConfigEntity configEntity = findEntitys(param).get(0);
        Integer smsCount = configEntity.getSmsCount();
        Integer sentCount = configEntity.getSentCount();
        if (null == sentCount) {
            sentCount = 0;
        }
        smsCount -= counts;
        sentCount += counts;
        configEntity.setSmsCount(smsCount);
        Double smsBalance = configEntity.getSmsBalance() == null ? 0D : configEntity.getSmsBalance() - counts * 0.05;
        configEntity.setSmsBalance(smsBalance);
        configEntity.setSentCount(sentCount);
        update(configEntity);
    }

    @Override
    public boolean availableSign(String corpid) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        //审批通过
        param.put("status", SmsSignStatusEnum.APPROVAL_SUCCESS.getCode());
        param.put("del", DelEnum.NORMAL.getDel());
        List<SmsConfigEntity> signList = findEntitys(param);
        if (signList.size() == 0) {
            return false;
        }
        return true;
    }

    @Override
    public Integer getSmsCounts(String corpid) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("del", DelEnum.NORMAL.getDel());
        List<SmsConfigEntity> signList = findEntitys(param);
        if (signList == null || signList.size() == 0) {
            return 0;
        }
        SmsConfigEntity signEntity = signList.get(0);
        return signEntity.getSmsCount();
    }

    @Override
    public SmsConfigEntity getEntityByCorpid(String corpid) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("del", DelEnum.NORMAL.getDel());
        List<SmsConfigEntity> signList = findEntitys(param);
        if (signList == null || signList.size() == 0) {
            return null;
        }
        return signList.get(0);
    }
}
